$(function () {
    var sid = getParameter("sid");
    $.post("/music/singer/getOne",{sid:sid},function (data) {
        console.log(data)
        $("#singerName").html("歌手"+data.singername);
        load(sid);
    });
    $("#upLoadMusic").click(function (){
        location.href="music_add.html?sid="+sid;
    })
});

function load(sid,currentPage) {
    $.get("/music/singer/findSingerMusicByPage",{sid:sid,currentPage:currentPage},function (data) {
        console.log("数据："+data)
        if (data.list!=null){
            //获取tbody
            $tbody = $("#musicListMsg");
            //拼接
            var trlist = '';
            for (var i = 0; i < data.list.length; i++) {
                var tr ="<tr>" +
                    "       <td>" +
                    "           <label class='lyear-checkbox checkbox-primary'>" +
                    "           <input type='checkbox' name='mlids' value='"+data.list[i].mlid+"' " +
                    "                     id='"+data.list[i].mlid+"' class='itemSelect'> <span></span>" +
                    "       </label>" +
                    "       </td>" +
                    "       <td>"+((data.currentPage-1)*data.pageSize+i+1)+"</td>" +
                    "       <td>"+data.list[i].musicname+"</td>" +
                    "       <td>" +
                    "           <div class='btn-group'>" +
                    "           <a class='btn btn-xs btn-default' href='music_update.html?mid="+data.list[i].mid+"' title='编辑' data-toggle='tooltip'>" +
                    "               <i class='mdi mdi-pencil'></i>" +
                    "           </a>" +
                    "           <a class='btn btn-xs btn-default' href='javascript:deleteMusic("+data.list[i].mid+")' title='删除' data-toggle='tooltip'>" +
                    "               <i class='mdi mdi-window-close'></i>" +
                    "           </a>" +
                    "           </div>" +
                    "       </td>" +
                    "</tr>"
                trlist += tr;
            }
            //2.3 将liList添加到ul中
            $tbody.html(trlist);
            //4.处理页面 前5后4
            //4.1 开始页数 = 当前页数-5
            var start = data.currentPage-5;
            //4.2结束页数=当前页数+4
            var end = data.currentPage+4;
            //4.3判断前面不足5个的情况
            if (start<1){
                start=1;
                end=10;
            }
            //4.4判断后面不足4的情况
            if (end>data.totalPage){
                end = data.totalPage;
                start = end-9;
            }
            //4.5总页数不足10页
            if (data.totalPage<10){
                start = 1;
                end = data.totalPage;
            }
            //4.6拼接 ul -li
            $pageUl = $("#pageUl");
            //首页  cid = 3
            var pageList = '<li><a href="javaScript:load('+sid+',1)">首页</a></li>';
            //上一页
            var prePage = data.currentPage - 1;
            if(prePage < 1){
                prePage = 1;
            }
            pageList += '<li >' +
                '<a href="javaScript:load('+sid+','+prePage+')">上一页</a></li>';
            //拼接 页码 从start开始 end 结束
            for(var i = start ; i <= end; i++){
                if(i === data.currentPage){
                    pageList += '<li onclick="load('+sid+','+i+')" class="active"><a href="javaScript:">'+i+'</a></li>';
                }else {
                    pageList += '<li onclick="load('+sid+','+i+')" ><a href="javaScript:">'+i+'</a></li>';
                }
            }
            //下一页
            var nextPage = data.currentPage + 1;
            if (nextPage > data.totalPage){
                nextPage = data.totalPage;
            }
            pageList += '<li onclick="load('+sid+','+nextPage+')" ><a href="javascript:;">下一页</a></li>';
            //末页
            pageList += '<li onclick="load('+sid+','+data.totalPage+')" ><a href="javascript:;">末页</a></li>';
            //4.7 将pageList添加到ul中
            $pageUl.html(pageList);
        }
    },"json");
}
function deleteMusic(mid) {
    var sid = getParameter("sid");
    if (confirm("确定要删除吗？")) {
        $.post("/music/music/delete",{mid:mid},function (data) {
            location.href="music_list.html?sid="+sid;
        });
    }
}
